//
//  TabBarView.m
//  PetAPP
//
//  Created by dllo on 16/1/22.
//  Copyright © 2016年 Yuming. All rights reserved.
//

#import "TabBarView.h"
#import "TableBarTouchView.h"

@interface TabBarView ()

@property (nonatomic, retain) TableBarTouchView *viewOfHome;

@property (nonatomic, retain) TableBarTouchView *viewOfshop;

@property (nonatomic, retain) TableBarTouchView *viewOfserver;

@property (nonatomic, retain) TableBarTouchView *viewOfmy;

@property (nonatomic, assign) NSInteger index;

@property (nonatomic, copy) void (^block)(NSInteger);

@end

@implementation TabBarView



- (instancetype)initWithFrame:(CGRect)frame {

    
    self = [super initWithFrame:frame];
    if (self) {
        CGRect newFrame = (CGRect){.size = frame.size};
        
        [self createSmallViewWithFrame:frame];
        [self createSubViewsWithFrame:newFrame];
    }
    return self;
}
#pragma mark - 创建灰线
- (void)createSmallViewWithFrame:(CGRect)frame {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 1)];
    [self addSubview:view];
    view.layer.borderWidth = 0.5;
    view.layer.borderColor = [UIColor grayColor].CGColor;
    [view release];
}
#pragma mark - 创建
- (void)createSubViewsWithFrame:(CGRect)frame {
    self.index = 1;
    CGFloat width = frame.size.width;
    CGFloat height = frame.size.height;
    
    /** 创建首页按钮 */
    self.viewOfHome = [[TableBarTouchView alloc] initWithFrame:CGRectMake(30, 5, 30, height - 10) image:@"home"];
    [self addSubview:self.viewOfHome];
    [_viewOfHome release];
    self.viewOfHome.tag = 1;
    
    [self.viewOfHome passTag:^(TableBarTouchView *view) {
        [self changeImageWith:view];
    }];
    
    self.viewOfHome.imageView.image = [UIImage imageNamed:@"home2"];
    
    /** 创建商店按钮 */
    self.viewOfshop = [[TableBarTouchView alloc] initWithFrame:CGRectMake(100, 5, 30, height - 10) image:@"shop"];
    [self addSubview:self.viewOfshop];
    [_viewOfshop release];
    self.viewOfshop.tag = 2;
    
    [self.viewOfshop passTag:^(TableBarTouchView *view) {
        [self changeImageWith:view];
    }];
    
    /** 创建照相机按钮 */
    
    /** 创建服务按钮 */
    self.viewOfserver = [[TableBarTouchView alloc] initWithFrame:CGRectMake(width - 130, 5, 30, height - 10) image:@"server"];
    [self addSubview:self.viewOfserver];
    [_viewOfserver release];
    self.viewOfserver.tag = 3;
    
    [self.viewOfserver passTag:^(TableBarTouchView *view) {
        [self changeImageWith:view];
    }];
    
    /** 创建我的页面按钮 */
    self.viewOfmy = [[TableBarTouchView alloc] initWithFrame:CGRectMake(width - 60, 5, 30, height - 10) image:@"my"];
    [self addSubview:self.viewOfmy];
    [_viewOfmy release];
    self.viewOfmy.tag = 4;
    
    [self.viewOfmy passTag:^(TableBarTouchView *view) {
        [self changeImageWith:view];
    }];
    
}

- (void)changeImageWith:(TableBarTouchView *)view{
    if (view.tag != self.index) {
        TableBarTouchView *tempView = [self viewWithTag:self.index];
        tempView.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@1",tempView.imageName]];
        self.index = view.tag;
        view.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@2",view.imageName]];
        self.block(view.tag);
    }
}

- (void)changeControllerViewWithTag:(void (^)(NSInteger))block {

    self.block = block;

}


- (void)dealloc {
    [_viewOfHome release];
    [super dealloc];
}
@end
